package main

// import "os"
import "fmt"

import "⚛sdl"
import "⚛sdl/ttf"
// import "keys"

import "panel"
import "edit"
import "header"
import "frame"

func init() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 { panic(sdl.GetError()) }
	if ttf.Init() != 0 { panic(sdl.GetError()) }
}

func main() {
	screen := sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)
	if screen == nil { panic(sdl.GetError()) }
	sdl.EnableUNICODE(1)
	sdl.WM_SetCaption("mytide", "17")
    loop(screen)
}

type ignore int

func (ignore) Do(command string) {}

func loop(con *sdl.Surface) {
    cols := 100
    rows := 20
//
    c := &panel.ScrollablePanel{}
    h := header.NewHeaderPanel(nil, nil)
    e := edit.NewEditCompilePanel(c, h)
    h.Other = e.Edit
    h.Do = e.Edit
    f := frame.NewFrame(e.Edit, e, c, h)
    s := panel.NewShelf(f, frame.NewFrame(ignore(0), edit.NewEditCompilePanel(nil, nil), nil, nil))
    wp := panel.NewWindowPanel(con,s)
    wp.Resize(rows, cols, 0, 0)
    wp.Refresh()
//
    var focus panel.Type = e.Edit
    for {
    	wp.ShowCursor()
    	wp.Flip()
 		event := <- sdl.Events
 		wp.HideCursor()	
 		wp.Flip()
 		switch e := event.(type) {
 			case sdl.QuitEvent:
 				return
 				
 			case sdl.ResizeEvent:
 				fmt.Println("RESIZE: w, h = ", int(e.W), int(e.H))
 				wp.ResizeInPixels(int(e.W), int(e.H))
 				wp.Refresh()
 				
 			case sdl.KeyboardEvent:
 				if e.Type == sdl.KEYUP {
 					key := e.Keysym.Sym
 					if e.Keysym.Mod & sdl.KMOD_LCTRL != 0 { key = key - 'a' + 1 }
 					if e.Keysym.Mod & sdl.KMOD_LSHIFT != 0 {  key = key - 'a' + 'A' }
	 				fmt.Println("keyboard event: ", key)
	 				newFocus := focus.Key(int(key))
	        		if newFocus != nil { focus = newFocus }
	        		focus.Focus()
	        	}
 				
 			case sdl.MouseMotionEvent:
 				// nothing at the moment
 		}   
 		wp.Flip()
//    	reply, err := con.WaitForEvent()
//    	if err != nil { fmt.Printf("boom: %v\n", err ); os.Exit(1) }
//    	fmt.Printf(">> event %v\n", reply)
//    	switch event := reply.(type) {
//    	case xgb.KeyReleaseEvent:
//    		newFocus := focus.Key(keys.KeyCodeToChar(&event))
//    		if newFocus != nil { focus = newFocus }
//    		
//    	case xgb.ExposeEvent:
//    		wp.Refresh()		
//        }
    }
}



